package com.info.zhiduoduo.repository.mapper.company;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.info.zhiduoduo.repository.dto.crowdsource.CrowdsourceMonthlyDepositDto;
import com.info.zhiduoduo.repository.entity.company.CrowdsourceMonthlyDepositRecord;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author bob
 * @since 2021-01-08
 */
public interface CrowdsourceMonthlyDepositRecordMapper extends BaseMapper<CrowdsourceMonthlyDepositRecord> {

	@Select(" select cmd.*,c.compname,c.return_period,sm.managername as adminName " +
			" from zdd_crowdsource_monthly_deposit_record cmd " +
			" join zdd_company c on c.compid = cmd.compid and c.compname like '%${name}%' " +
			" left join zdd_system_manager sm on sm.managerid = cmd.adminid " +
			" where if(#{state}=0, 1=1, cmd.state=#{state}) " +
			" and IF(ISNULL(#{startDate}), 1=1, cmd.month >= date_format(#{startDate}, '%Y-%m-01')) " +
			" and IF(ISNULL(#{endDate}), 1=1, cmd.month <= date_format(#{endDate}, '%Y-%m-01')) " +
			" order by cmd.month desc")
	List<CrowdsourceMonthlyDepositDto> listByDate(@Param("name") String name, @Param("state") int state,
	                                              @Param("startDate") Date startDate,
	                                              @Param("endDate") Date endDate);

	/**
	 * 查询企业未销帐保证金总额
	 *
	 */
	@Select(" select sum(should_return_deposit) from zdd_crowdsource_monthly_deposit_record where compid = #{compid} and state = 1")
	BigDecimal companyCancelDepositTotalAmount(@Param("compid") Integer compid);

	/**
	 * 查询数据是否已存在
	 *
	 */
	@Select(" select * from zdd_crowdsource_monthly_deposit_record " +
			" where compid = #{compid} " +
			" and month = #{startDate} ")
	CrowdsourceMonthlyDepositRecord selectMonthData(@Param("compid") Integer compid, @Param("startDate") Date startDate);
}
